﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HREDU.Hoang_Tuan.Classes;
using System.IO;

namespace HREDU.Hoang_Tuan
{
    public partial class frmLogin : Form
    {
        public bool LoginOK;
        public frmLogin()
        {
            InitializeComponent();
        }


        private void frmLogin_Load(object sender, EventArgs e)
        {
            MITI.BaseDB.Instance.g_ConnectStr = MITI.BaseDB.Instance.GetConnectStrFromFile(Application.StartupPath + "\\DBConnect.udl");
            lblCSDL.Text = MITI.BaseDB.Instance.LayGiaTriThanhPhan("Initial Catalog");
            lblServer.Text = MITI.BaseDB.Instance.LayGiaTriThanhPhan("Data Source");
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("Tên đăng nhập chưa có.");
                return;
            }

            MITI.BaseDB.Instance.g_UserName = this.txtUserName.Text;
            MITI.BaseDB.Instance.g_Password = this.txtPassword.Text;

            if (!CUsers.DuocPhepChay(this.txtUserName.Text, this.txtPassword.Text))
            {
                LoginOK = false;
                if (MessageBox.Show("Đăng nhập không được.Đăng nhập lại?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    this.txtUserName.Text = "";
                    this.txtPassword.Text = "";
                }
            }
            else
            {
                LoginOK = true;
                //Đóng form Login lai khi thành công 
                this.Close();

                //InitSQLDMO()
                //g_User = CUsers.GetUserByName(g_UserName)

                //lay ra danh sach cac quyen cua nguoi dung do tren form main
                //truoc tien la xoa het cac quyen cua nguoi su dung truoc
                //dsQuyenTrenForm.Clear();

                ////gan cau lenh ket noi sao cho chi dung cho SQLServer
                //XL_DULIEU.GanCauLenhKetNoi(myConnStr.Substring(myConnStr.IndexOf(";") + 1));
                ////thay the chuoi ket noi bang lenh user name va password hien tai
                //XL_DULIEU.GanCauLenhKetNoi(strLoginName_SA, strPassword_SA);
                //DataTable bangDSQuyen = new DataTable();
                //object cauLenhLayDuLieu = "select mn.VBMenuName from tblUser nsd, tblNHOM n, tblMENU mn, tblNHOM_MENU n_mn where nsd.loginname ='" + g_UserName + "'";
                //cauLenhLayDuLieu += " and nsd.ban_id =n.nhom_id and n.nhom_id = n_mn.nhom_id and n_mn.menu_id = mn.menu_id";
                //XL_DULIEU.LayDuLieuRaBang(cauLenhLayDuLieu, bangDSQuyen);
                //foreach (DataRow dong in bangDSQuyen.Rows)
                //{
                //    dsQuyenTrenForm.Add(dong("VBMenuName"));
                //}
                //objConnection.Close();
                //Đóng form Login lai khi thành công 
                //this.Close();
            }
        }

       

    }
}